package com.example.offer.no0024.impl;

import java.util.Stack;

import com.example.offer.no0024.ListNode;
import com.example.offer.no0024.Solution;

/**
 * @author yumuhui <yumuhui@kuaishou.com>
 * 时间复杂度：O(n)，其中 n 是链表的长度。需要遍历链表一次。
 * 空间复杂度：O(1)。
 * Created on 2021-05-17
 */
public class SolutionImpl implements Solution {
    @Override
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}
